package com.jason.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Student;
import com.jason.system.entity.Tool;
import com.jason.system.service.ToolService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/1 21:24
 */

@RestController
@RequestMapping("/api/tool")
@Api(value = "/tool", tags = "工具接口")
public class ToolController {

  private static final Logger logger = LoggerFactory.getLogger(ToolController.class);

  @Autowired
  ToolService toolService;

  @ApiOperation(value ="获取工具", notes = "获取所有工具信息")
  @ApiImplicitParams(
          {@ApiImplicitParam(paramType = "query",name = "page", value = "页数",required = true, dataType = "Integer"),
                  @ApiImplicitParam(paramType = "query",name = "size",value = "每页个数",required = true, dataType = "Integer")}
  )
  @ApiResponses({
          @ApiResponse(code = 200,message = "获取所有工具信息成功"),
          @ApiResponse(code = 201,message = "获取所有工具信息失败")
  })
  @GetMapping("/getAll")
  public JSONObject getAllNumbers(@RequestParam("page") Integer page , @RequestParam("size") Integer size){
    logger.info("==>>>toolController==>>>进行路径：/api/tool/getAll.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    Pager<Tool> tools = toolService.getAllTools(page,size);
    logger.info("==>>>toolController==>>>获取tools对象: {}",tools);
    if (tools!=null){
      logger.info("==>>>toolController==>>>获取所有tools信息成功.....");
      r.setCode(200);
      r.setMessage("获取所有工具信息成功");
      r.setData(tools);
    }else {
      logger.info("==>>>toolController==>>>获取所有tools信息失败.....");
      r.setCode(200);
      r.setMessage("获取所有工具信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="添加工具", notes = "添加工具信息")
  @ApiImplicitParams(
          {@ApiImplicitParam(paramType = "query",name = "toolNumber", value = "工具编号",required = true, dataType = "String"),
                  @ApiImplicitParam(paramType = "query",name = "toolName",value = "工具名字",required = true, dataType = "String"),
                  @ApiImplicitParam(paramType = "query",name = "url",value = "图片路径",required = true, dataType = "String")
          }
  )
  @ApiResponses({
          @ApiResponse(code = 200,message = "添加工具信息成功"),
          @ApiResponse(code = 201,message = "添加工具信息失败")
  })
  @PostMapping("/insert")
  public JSONObject insertTool(Tool insertTool){
    logger.info("==>>>toolController==>>>进行路径：tool:{}",insertTool);
    logger.info("==>>>toolController==>>>进行路径：/api/tool/insert.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>toolController==>>>正在获取tools信息.....");
    Tool tool = toolService.getToolByNumber(insertTool.getToolNumber());
    logger.info("==>>>toolController==>>>获取tool对象: {}",tool);
    if(tool!=null){
      logger.info("==>>>toolController==>>>tools信息已存在，勿重复添加.....");
      r.setCode(201);
      r.setMessage("工具已存在，勿重复添加");
    }else {
      toolService.insertTool(insertTool);
      logger.info("==>>>toolController==>>>添加tools信息完成.....");
      r.setCode(200);
      r.setMessage("添加成功");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="更新工具", notes = "更新工具信息")
  @ApiImplicitParams(
          {@ApiImplicitParam(paramType = "query",name = "toolNumber", value = "工具编号",required = true, dataType = "String"),
                  @ApiImplicitParam(paramType = "query",name = "toolName",value = "工具名字",required = true, dataType = "String"),
                  @ApiImplicitParam(paramType = "query",name = "url",value = "图片路径",required = true, dataType = "String")
          }
  )
  @ApiResponses({
          @ApiResponse(code = 200,message = "更新工具信息成功"),
          @ApiResponse(code = 201,message = "更新工具信息失败")
  })
  @PostMapping("/update")
  public JSONObject updateTool(Tool updateTool){
    logger.info("==>>>toolController==>>>进行路径：/api/tool/update.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>toolController==>>>正在更新tool信息中.....");
    try {
      toolService.updateTool(updateTool);
      logger.info("==>>>toolController==>>>更新tool信息完成.....");
      r.setCode(200);
      r.setMessage("更新tool信息成功");
    }catch (Exception e){
      logger.info("==>>>toolController==>>>更新tool信息失败.....");
      r.setCode(201);
      r.setMessage("更新tool信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="删除工具", notes = "删除工具信息")
  @ApiImplicitParam(paramType = "query",name = "tId", value = "工具id",required = true, dataType = "Integer")
  @ApiResponses({
          @ApiResponse(code = 200,message = "删除工具信息成功"),
          @ApiResponse(code = 201,message = "删除工具信息失败")
  })
  @PostMapping("/delete/{tId}")
  public JSONObject delete(@PathVariable("tId") Integer tId){
    logger.info("==>>>toolController==>>>进行路径：/api/tool/delete.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>toolController==>>>正在删除tool信息中.....");
    boolean result = toolService.deleteToolById(tId);
    if (result){
      logger.info("==>>>toolController==>>>删除tool信息完成.....");
      r.setCode(200);
      r.setMessage("删除tool信息成功");
    }else {
      logger.info("==>>>toolController==>>>删除删除tool信息失败.....");
      r.setCode(201);
      r.setMessage("删除tool信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="批量删除工具", notes = "批量删除工具信息")
  @ApiImplicitParam(paramType = "query",name = "tIds", value = "工具id,以逗号分割",required = true, dataType = "String")
  @ApiResponses({
          @ApiResponse(code = 200,message = "批量删除工具信息成功"),
          @ApiResponse(code = 201,message = "批量删除工具信息失败")
  })
  @PostMapping("/deleteTools")
  public JSONObject deleteNumbers(@RequestParam("tIds") String tIds){
    logger.info("==>>>toolController==>>>进行路径：/api/tool/deleteTools.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>toolController==>>>正在批量删除tools中.....");
    boolean result = toolService.deleteTools(tIds);
    if (result){
      logger.info("==>>>toolController==>>>批量删除tool信息完成.....");
      r.setCode(200);
      r.setMessage("批量删除tool信息成功");
    }else {
      logger.info("==>>>toolController==>>>批量删除tool信息失败.....");
      r.setCode(201);
      r.setMessage("批量删除tool信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @PostMapping("/search")
  public JSONObject searchStudent(Integer page , Integer size , String toolName){
    logger.info("==>>>toolController==>>>进行路径：/api/tool/searchTools.....");
    logger.info("==>>>toolController==>>>toolName:{}",toolName);
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>toolController==>>>正在查询Tools中.....");
    Pager<Tool> toolPager = toolService.getToolByName(page,size,toolName);
    logger.info("==>>>toolController==>>>tools:{}",toolPager);
    if (toolPager!=null){
      logger.info("==>>>toolController==>>>查询tools完成.....");
      r.setCode(200);
      r.setMessage("查询工具信息成功");
      r.setData(toolPager);
    }else {
      logger.info("==>>>toolController==>>>查询tools失败.....");
      r.setCode(201);
      r.setMessage("查询工具信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }
}
